package com.zj.docker.service.impl;

import com.zj.docker.Repository.VolumesRepository;
import com.zj.docker.domain.Containers;
import com.zj.docker.domain.Volumes;
import com.zj.docker.service.ContainersService;
import com.zj.docker.service.VolumesService;
import com.zj.docker.vo.VolumesVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: zj
 * @Date: 2018/12/11 16:30
 * @Description:
 */
@Service
@Slf4j
public class VolumesServiceImpl implements VolumesService{
    @Autowired
    VolumesRepository volumesRepository;
    @Autowired
    ContainersService containersService;
    @Override
    public Volumes SelectOneByVolumeName(String name) {
        return volumesRepository.findOneByName( name );
    }

    @Override
    public Page<Volumes> SelectAllByContainerId(String containerId,Integer pageIndex) {
        Pageable pageable = PageRequest.of( pageIndex,5);

        Page<Volumes> page =volumesRepository.findAllByContainerId( containerId,pageable );
        return page;
    }

    @Override
    public List<Volumes> SelectAllByContainerId(String containerId) {
        List<Volumes> list = volumesRepository.findAllByContainerId( containerId );
        return list;
    }

    @Override
    public List<VolumesVo> SelectAllVolumesByAuthor(String author) {
        List<VolumesVo> result =new ArrayList<>(  );
        List<Containers> containers =containersService.listAllContainer( author );
        for (Containers container:containers) {
            List<Volumes> volumes= this.SelectAllByContainerId( container.getContainerId() );

            for (Volumes volume:volumes) {
                VolumesVo volumesVo=new VolumesVo();
                volumesVo.setId( volume.getId() );
                volumesVo.setContainerId( volume.getContainerId() );
                volumesVo.setUpdateDate( volume.getUpdateDate() );
                volumesVo.setSource( volume.getSource() );
                volumesVo.setName( volume.getName() );
                volumesVo.setDestination(  volume.getDestination());
                volumesVo.setCreateDate(  volume.getCreateDate());
                volumesVo.setContainerName(  container.getContainerName());
                result.add( volumesVo );
            }
//
        }
        return result;
    }

    @Override
    public Page<Volumes> SelectAllVolumes(Integer pageIndex) {
        Pageable pageable=PageRequest.of( pageIndex,5);
        Page<Volumes> volumes = volumesRepository.findAll( pageable );
        return volumes;
    }

    @Override
    public List<Volumes> SelectAllVolumes() {
        return volumesRepository.findAll();
    }
}
